<template>
    <div>
        <el-card header="请先登录" class="login-card">
            <el-form @submit.native.prevent="login"> <!--阻止默认提交跳转页面 -->
                <el-form-item label="用户名">
                    <el-input v-model="model.username"></el-input>
                </el-form-item>
                <el-form-item label="密码">
                    <el-input type="password" v-model="model.password"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" native-type="submit">登陆</el-button>
                </el-form-item>
            </el-form>
        </el-card>
    </div>
</template>

<script>
export default {
    data(){
        return{
            model:{}
        }
    },
    methods: {
        async login(){
            const res=await this.$http.post('login',this.model)
            localStorage.token=res.data.token//将数据放在localStorage是浏览器的存储，重新打开浏览器之后还能访问得到
            // sessionStorage.token=res.data.token//当前浏览器关闭之后token就没了
           this.$router.push('/')
           this.$message({
               type:"success",
               message:'登陆成功'
           })
        }
    },
}
</script>

<style scoped>
    .login-card{
        width: 25rem;
        margin: 5rem auto;
    }
</style>